package com.zp.common.base.resp;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.zp.common.base.constant.ResultCode;
import lombok.Data;


/**
 * 全局响应类
 *
 */
@Data
public class BaseResp<T> {
    private static final String DATE_TIME = "yyyy-MM-dd HH:mm:ss";
    /** 返回码 */
    private String code;

    /** 返回信息描述 */
    private String message;

    /** 返回数据 */
    private T data;

    /**
     * 总页数
     *
     * @return
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long totalPage;

    /**
     * totalRecord 总条数
     *
     * @return
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long totalRecord;

    public BaseResp() {}
    /**
     * @param code 状态码
     * @param message 信息
     * @param data 数据
     */
    public BaseResp(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
    /**
     * @param code 状态码
     * @param message 信息
     */
    public BaseResp(String code, String message) {
        this.code = code;
        this.message = message;
    }
    /**
     * 不带数据的返回结果
     *
     * @param commonEnum
     */
    public BaseResp(ResultCode commonEnum) {
        this.code = commonEnum.getCode();
        this.message = commonEnum.getMsg();
    }
    /**
     * 带数据的返回结果
     *
     * @param commonEnum
     * @param data
     */
    public BaseResp(ResultCode commonEnum, T data) {
        this.code = commonEnum.getCode();
        this.message = commonEnum.getMsg();
        this.data = data;
    }


    /**
     * 失败
     * @param errorInfo
     */
    public static BaseResp error(ResultCode errorInfo) {
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(errorInfo.getCode());
        baseResp.setMessage(errorInfo.getMsg());
        return baseResp;
    }

    /**
     * 失败
     */
    public static BaseResp error(String code, String message) {
        BaseResp baseResp = new BaseResp();
        baseResp.setCode(code);
        baseResp.setMessage(message);
        return baseResp;
    }
}
